In [ ]:
a = [1,2,3,4,5,6,7]
def adding(x):
total = 0
for i in x:
total += i
print(total)
total = 0
i = 0
while i < len(x):
total += x[i]
i += 1
print(total)
return
adding(a)
In [ ]:
b = [1,2,3,4,5,6,7]
def recur(lst,counter,total):
if counter == len(lst):
print(total)
return
else:
total += lst[counter]
counter += 1
return recur(lst,counter,total)
recur(b,0,0)
In [ ]:
lst1=['a','b','c']
lst2=[1,2,3]
def combine(lst1,lst2):
lst = []
i = 0
while i < len(lst1):
lst.append(lst1[i])
lst.append(lst2[i])
i += 1
lst
print(lst)
return
combine(lst1,lst2)
Write a function that computes the list of the first 100 Fibonacci numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.
In [ ]:
# fibonacci numbers 1-100
def fibonacci(i):
k = 0
num0 = 0
num1 = 1
print(num0)
print(num1)
while k < i:
next_num = num0 + num1
print(next_num)
num0 = num1
num1 = next_num
k += 1
fibonacci(98)
In [ ]:
# fibonacci numbers 1-100
def fibonacci(i):
fib = [0,1]
k = 0
while len(fib) < i:
next_num = fib[k] + fib[k+1]
fib.append(next_num)
k+=1
print(fib)
fibonacci(100)
In [ ]:
98750301291
In [7]:
#this will search for the whole list 1x for i_dwn
def list_sort(lst):
iterations = 0
while 1:
i = 0
j = i+1
value_before = make_num(lst[:])
while j < len(lst):
n0 = (int(str(lst[i]) + str(lst[j])))
n1 = (int(str(lst[j]) + str(lst[i])))
print('lst:',lst)
if n0 < n1:
lst[i],lst[j] = lst[j],lst[i]
i+=1
j=i+1
iterations += 1
value_after = make_num(lst[:])
if value_before < value_after:
continue
else:
print('before:',value_before)
print('after:',value_after)
print('iterations:',iterations)
break
return
# function to print the list as a number
def make_num(lst_sorted):
s_total = ''
for i in lst_sorted:
s_total += str(i)
return int(s_total)
#lst = [5, 50, 56]
lst = [17, 32, 91, 7, 46]
# this calls the initial sort
list_sort(lst)
In [ ]: